package com.example.test.model.json_analyzer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class NetJsonAnalyzer {
   //检测是否返回的是错误json
	public static int isErrorJson(String json)
	{
		try {
			JSONObject object =new JSONObject(json);
			 if(object.has("error"))
			 {
				 
				 return (Integer)object.get("error_code");
			 }
			 else return 0;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
		
	}
	
	public static void subJsonToHashArr(String subJson,HashMap<String, String> map,String preStr)
	{
		try {
			JSONObject jsonObject =new JSONObject(subJson);
			Iterator< String> itKeys = jsonObject.keys();
			String key;
			 while(itKeys.hasNext())
             {
          	   key=(String)itKeys.next();
          	   if(isJson(jsonObject.getString(key)))
          	   {
          		   subJsonToHashArr(jsonObject.getString(key),map,preStr+key);
          	   }
          	   else
          	   {
          	   map.put(preStr+key,jsonObject.getString(key));
          	   }
             }
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static List<HashMap<String, String>> JsonToHasArr(String json,String jsonname)
   {
	   try {  
		   JSONObject object =new JSONObject(json);
           JSONArray jsonArrays = new JSONArray(object.getString(jsonname));  
           List<HashMap<String, String>> listArrays =new ArrayList<HashMap<String, String>>();
           for (int i = 0; i < jsonArrays.length(); i++) {  
               JSONObject jsonObject = jsonArrays.getJSONObject(i);  
               Iterator<String> itKeys = jsonObject.keys();
               HashMap<String, String> map = new HashMap<String, String>();  
               String key="";
               map.put("resource", "netease");
               while(itKeys.hasNext())
               {
            	   key=(String)itKeys.next();
            	   if(isJson(jsonObject.getString(key)))
            	   {
            		   subJsonToHashArr(jsonObject.getString(key), map, key);
            	   }
            	   else
            	   {
            	   map.put(key,jsonObject.getString(key));
            	   }
               }
              
               listArrays.add(map);  
               
           }  
           return listArrays;
       } catch (JSONException e) {  
           // TODO Auto-generated catch block  
           System.out.println(e.getMessage());  
       }   
	   return null;
   }
   
 
   public static void main(String args[])
   {
	   String content;
	   File file=new File("D:\\1.txt");
	   FileInputStream inputStream;
	try {
		inputStream = new FileInputStream(file);
		   byte []buf =new byte[(int)file.length()];
		   int count =inputStream.read(buf);
		   inputStream.close();
		   if(count!=-1)
		   {
			   content=new String(buf,"utf-8");
			   List<HashMap<String, String>> lArrTimeline =JsonToHasArr(content,"statuses");
			   for (HashMap<String, String> statusMap : lArrTimeline) {
				System.out.println(lArrTimeline.size());
				   Set<String> tempSet = statusMap.keySet();
				
				for (String key : tempSet) {
					System.out.print(key+":"+statusMap.get(key));
				}
				System.out.println("");
			}
		   }
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
	   
   }
   
   public static HashMap<String, String> JsonToHash(String json)
	{
		 JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(json);
			Iterator<String> itKeys = jsonObject.keys();
	         HashMap<String, String> map = new HashMap<String, String>();  
	         String key="";
	         while(itKeys.hasNext())
	         {
	      	   key=(String)itKeys.next();
	      	   map.put(key,jsonObject.getString(key));
	         }
	         return map;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
         return null;
	}
   
   public static void JsonAddToHash(HashMap<String, String> map, String json)
 	{
 		 JSONObject jsonObject;
 		try {
 			jsonObject = new JSONObject(json);
 			Iterator<String> itKeys = jsonObject.keys(); 
 	         String key="";
 	         while(itKeys.hasNext())
 	         {
 	      	   key=(String)itKeys.next();
 	      	   map.put("user"+key,jsonObject.getString(key));
 	         }
 		} catch (JSONException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} 
 	}
   public static boolean isJson(String value) {  
       try {  
           new JSONObject(value);  
       } catch (JSONException e) {  
           return false;  
       }  
       return true;  
   }  
}



